package com.koron.order.concession.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.util.CodeTools;
import com.koron.order.common.tools.CommonCodeTools;
import com.koron.order.common.tools.OrderCodeEnum;
import com.koron.order.concession.bean.convertor.ConcessionConvertor;
import com.koron.order.concession.bean.dto.ConcessionDto;
import com.koron.order.concession.bean.dto.ConcessionStatus;
import com.koron.order.concession.bean.po.ConcessionInfluencePo;
import com.koron.order.concession.bean.po.ConcessionPo;
import com.koron.order.concession.bean.query.ConcessionQuery;
import com.koron.order.concession.bean.vo.ConcessionVo;
import com.koron.order.concession.mapper.ConcessionMapper;
import com.koron.order.concession.service.ConcessionInfluenceService;
import com.koron.order.concession.service.ConcessionService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 让步接收单信息主表(Concession)表服务实现类
 *
 * @author zhouj
 * @since 2022-01-14 09:51:53
 */
@Service("concessionService")
public class ConcessionServiceImpl implements ConcessionService {

    @Autowired
    ConcessionConvertor concessionConvertor;

    @Autowired
    ConcessionInfluenceService concessionInfluenceService;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public ConcessionPo insertOrUpdate(SessionFactory factory, ConcessionDto concessionDto) {

        //新增时,区分好
        ConcessionPo concessionPo = concessionConvertor.concessionDtoToPo(concessionDto);

        ConcessionMapper mapper = factory.getMapper(ConcessionMapper.class);
        if (StringUtils.isEmpty(concessionPo.getId())) {
            concessionPo.setId(CodeTools.getCode32());
            concessionPo.setCode(CommonCodeTools.getNormalOrderCode(concessionPo.getOrderCode(), OrderCodeEnum.RB));
            concessionPo.setStatus(ConcessionStatus.DRAFT.getStatus());
        }
        //主表入库
        mapper.insertOrUpdate(concessionPo);

        List<ConcessionInfluencePo> concessionInfluencePoList = concessionDto.getConcessionInfluencePoList();
        if (CollectionUtils.isNotEmpty(concessionInfluencePoList)) {
            concessionInfluencePoList.forEach(p -> p.setConcessionId(concessionPo.getId()));
        }
        //从表入库
        concessionInfluenceService.insertBatch(factory, concessionDto.getConcessionInfluencePoList());
        return concessionPo;
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, ConcessionPo concessionPo) {

        ConcessionMapper mapper = factory.getMapper(ConcessionMapper.class);
        return mapper.update(concessionPo);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, ConcessionPo concessionPo) {

        ConcessionMapper mapper = factory.getMapper(ConcessionMapper.class);
        return mapper.insert(concessionPo);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        ConcessionMapper mapper = factory.getMapper(ConcessionMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public ConcessionPo queryById(SessionFactory factory, String id) {

        ConcessionMapper mapper = factory.getMapper(ConcessionMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<ConcessionVo> queryList(SessionFactory factory, ConcessionQuery concessionQuery) {

        ConcessionMapper mapper = factory.getMapper(ConcessionMapper.class);

        return mapper.queryList(concessionQuery);
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, ConcessionQuery concessionQuery, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy(false));
        ConcessionMapper mapper = factory.getMapper(ConcessionMapper.class);

        List<ConcessionVo> list = mapper.queryList(concessionQuery);

        return new PageInfo(list);
    }

    /*
     * 功能描述 提交工单状态接口
     * @author zhouj
     * @date 2021/12/21 10:55
     * @param [factory, orderId:工单id, status:将要改变的状态]
     * description 对于状态的修改需要进行顺序的判断,只能往后流转,
     * @return int
     */
    @Override
    @TaskAnnotation("submitConcession")
    public int submitConcession(SessionFactory factory, String orderId, String status) {

        ConcessionMapper mapper = factory.getMapper(ConcessionMapper.class);
        //根据工单id 先判断工单当时的状态
        ConcessionPo preConcessionPo = mapper.queryById(orderId);
        String preStatus = preConcessionPo.getStatus();

        //将要改变的状态
        ConcessionStatus concessionStatus = ConcessionStatus.getConcessionStatus(status);
        Assert.notNull(concessionStatus, "状态修改,枚举不能为空");

        if (StringUtils.isNotEmpty(preStatus)) {
            ConcessionStatus preConcessionStatus = ConcessionStatus.getConcessionStatus(preStatus);
            Assert.notNull(preConcessionStatus, "数据有误");
            Assert.isTrue(concessionStatus.getSort() - preConcessionStatus.getSort() == 1, "不可越级提交");
        } else {
            Assert.isTrue(StringUtils.equalsAny(concessionStatus.getStatus(),
                    ConcessionStatus.DRAFT.getStatus(),
                    ConcessionStatus.WAIT_REVIEW.getStatus()), "修改工单状态失败");
        }
        ConcessionPo po = new ConcessionPo();
        po.setId(orderId);
        po.setStatus(status);

        return mapper.update(po);
    }

}
